package com.jinyu.jyly.ui.mine;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.makeramen.roundedimageview.RoundedImageView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.jinyu.jyly.R;
import com.jinyu.jyly.base.BaseFragment;
import com.jinyu.jyly.bean.AllInfoBean;
import com.jinyu.jyly.bean.MineUserInfo;
import com.jinyu.jyly.ui.mine.account.AccountActivity;
import com.jinyu.jyly.ui.mine.address.ShipAddressListActivity;
import com.jinyu.jyly.ui.mine.buy.MineBuyActivity;
import com.jinyu.jyly.ui.mine.cashout.CashOutActivity;
import com.jinyu.jyly.ui.mine.invite.InviteActivity;
import com.jinyu.jyly.ui.mine.make_code.MakeCodeActivity;
import com.jinyu.jyly.ui.mine.newperson.NewPersonActivity;
import com.jinyu.jyly.ui.mine.order.OrderActivity;
import com.jinyu.jyly.ui.mine.passageway.PassageNewerWayActivity;
import com.jinyu.jyly.ui.mine.passageway.PassageWayActivity;
import com.jinyu.jyly.ui.mine.real_name.RealNameActivity;
import com.jinyu.jyly.ui.mine.recharge.RechargeMoneyActivity;
import com.jinyu.jyly.ui.mine.sell.MineSellActivity;
import com.jinyu.jyly.ui.mine.setting.SettingActivity;
import com.jinyu.jyly.ui.mine.suggest.SuggestActivity;
import com.jinyu.jyly.ui.mine.transfer.TransferAccountActivity;
import com.jinyu.jyly.utils.AES;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 个人中心
 */
public class MineFragment extends BaseFragment<MinePresenter> implements MineContract.View {
    @BindView(R.id.iv_header)
    RoundedImageView ivHeader;
    @BindView(R.id.tv_mobile)
    TextView tvMobile;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_aint)
    TextView tvAint;
    @BindView(R.id.tv_buy)
    TextView tvBuy;
    @BindView(R.id.tv_sell)
    TextView tvSell;
    @BindView(R.id.tv_order)
    TextView tvOrder;
    @BindView(R.id.tv_passageway)
    TextView tvPassageway;
    @BindView(R.id.tv_make_code)
    TextView tvMakeCode;
    @BindView(R.id.tv_new_jingpai)
    TextView tv_new_jingpai;
    @BindView(R.id.tv_harvest_address)
    TextView tvHarvestAddress;
    @BindView(R.id.tv_invite)
    TextView tvInvite;
    @BindView(R.id.ll_suggest)
    LinearLayout llSuggest;
    @BindView(R.id.ll_new_experience)
    LinearLayout llNewExperience;
    @BindView(R.id.ll_setting)
    LinearLayout llSetting;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.tv_rechange_btn)
    TextView tv_rechange_btn;
    @BindView(R.id.tv_tixian_btn)
    TextView tv_tixian_btn;
    @BindView(R.id.iv_level)
    ImageView iv_level;
    @BindView(R.id.tv_yuan_zhuan_btn)
    TextView tv_yuan_zhuan_btn;
    @BindView(R.id.tv_yuanbao)
    TextView tv_yuanbao;
    @BindView(R.id.tv_reputation)
    TextView tv_reputation;



    private MinePresenter presenter;
    private String card_name;
    private String card_num;
    private float pdb;
    private String mobile;
    private String realname;
    private String head_pic1;
    private float pdb_num;
    private float ingot;

    @Override
    protected View initView(ViewGroup container) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.fragment_mine, container, false);
        return view;
    }

    @Override
    protected void loadData() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                presenter.getUserView();
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    private void initData() {
        presenter.getUserView();
    }

    @Override
    public MinePresenter getPresenter() {
        if (presenter == null) {
            presenter = new MinePresenter(getActivity(), this);
        }
        return presenter;
    }


    @OnClick({R.id.tv_buy, R.id.tv_sell, R.id.tv_order, R.id.tv_passageway, R.id.tv_make_code, R.id.tv_new_jingpai, R.id.tv_harvest_address
            , R.id.tv_invite, R.id.ll_suggest, R.id.ll_new_experience, R.id.ll_setting, R.id.iv_account,
            R.id.tv_bao_zhuan_btn, R.id.tv_rechange_btn, R.id.tv_tixian_btn, R.id.ll_auth_user, R.id.tv_yuan_zhuan_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_buy:
                //我的买单
                Intent mbuyIntent = new Intent(getActivity(), MineBuyActivity.class);
                mbuyIntent.putExtra("type", "我的买单");
                startActivity(mbuyIntent);
                break;
            case R.id.tv_sell:
                //我的卖单
                Intent msellIntent = new Intent(getActivity(), MineSellActivity.class);
                msellIntent.putExtra("type", "我的卖单");
                startActivity(msellIntent);
                break;
            case R.id.tv_order:
                //提货订单
                Intent mOrderIntent = new Intent(getActivity(), OrderActivity.class);
                startActivity(mOrderIntent);
                break;
            case R.id.tv_passageway:
                //绿色通道
                Intent mpasswayIntent = new Intent(getActivity(), PassageWayActivity.class);
                startActivity(mpasswayIntent);
                break;
            case R.id.tv_make_code:
                //收款码
                Intent mCodeIntent = new Intent(getActivity(), MakeCodeActivity.class);
                startActivity(mCodeIntent);
                break;
            case R.id.tv_new_jingpai:
                //新人竞拍
                Intent intent = new Intent(getActivity(), PassageNewerWayActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_harvest_address:
                //收获地址
                Intent mAddressIntent = new Intent(getActivity(), ShipAddressListActivity.class);
                startActivity(mAddressIntent);
                break;
            case R.id.tv_invite:
                //邀请
                Intent mInviteIntent = new Intent(getActivity(), InviteActivity.class);
                startActivity(mInviteIntent);
                break;
            case R.id.ll_suggest:
                //意见
                Intent mSuggestIntent = new Intent(getActivity(), SuggestActivity.class);
                startActivity(mSuggestIntent);
                break;
            case R.id.ll_new_experience:
                //新人体验
                Intent mNewPersonIntent = new Intent(getActivity(), NewPersonActivity.class);
                startActivity(mNewPersonIntent);
                break;
            case R.id.ll_setting:
                //设置
                Intent mSettingIntent = new Intent(getActivity(), SettingActivity.class);
                mSettingIntent.putExtra("realname", realname);
                mSettingIntent.putExtra("mobile", mobile);
                mSettingIntent.putExtra("head_pic", head_pic1);
                startActivity(mSettingIntent);
                break;
            case R.id.iv_account:
                //账户菜场
                Intent accountIntent = new Intent(getActivity(), AccountActivity.class);
                accountIntent.putExtra("pdb", pdb);
                accountIntent.putExtra("ingot",ingot);
                startActivity(accountIntent);
                break;
            case R.id.tv_yuan_zhuan_btn:
                //元宝转账
                Intent mYuanIntent = new Intent(getActivity(), TransferAccountActivity.class);
                mYuanIntent.putExtra("type", 2);
                mYuanIntent.putExtra("pdb", ingot);
                startActivity(mYuanIntent);
                break;
            case R.id.tv_bao_zhuan_btn:
                //宝石转账
                Intent mTranIntent = new Intent(getActivity(), TransferAccountActivity.class);
                mTranIntent.putExtra("type", 1);
                mTranIntent.putExtra("pdb", pdb);
                startActivity(mTranIntent);
                break;
            case R.id.tv_rechange_btn:
                //充值
                Intent mRechargeMoneyIntent = new Intent(getActivity(), RechargeMoneyActivity.class);
                startActivity(mRechargeMoneyIntent);
                break;
            case R.id.tv_tixian_btn:
                //提现
                Intent mCashoutIntent = new Intent(getActivity(), CashOutActivity.class);
                mCashoutIntent.putExtra("pdb", pdb_num);
                startActivity(mCashoutIntent);
                break;
            case R.id.ll_auth_user:
                //实名认证
                Intent mRealIntent = new Intent(getActivity(), RealNameActivity.class);
                mRealIntent.putExtra("card_name", card_name);
                mRealIntent.putExtra("card_num", card_num);
                startActivity(mRealIntent);
                break;
        }
    }

    @Override
    public void onFailer(Throwable throwable) {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
    }

    /**
     * 用户详情
     *
     * @param infoBean
     */
    @Override
    public void onGetUserView(AllInfoBean infoBean) {
        refreshLayout.finishLoadMore();
        refreshLayout.finishRefresh();
        switch (infoBean.getStatus()) {
            case 200:
                String beanData = infoBean.getData();
                String result = null;
                try {
                    result = AES.decrypt(beanData);
                    Gson gson = new Gson();
                    MineUserInfo mineUserInfo = gson.fromJson(result, MineUserInfo.class);
                    setbaseInfo(mineUserInfo);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case 422:
                showToast(infoBean.getMsg());
                break;
            default:
                break;
        }
    }

    private void setbaseInfo(MineUserInfo mineUserInfo) {
        if (mineUserInfo != null) {
            tvMobile.setText(mineUserInfo.getMobile());
            tvName.setText(mineUserInfo.getRealname());
            tvAint.setText(mineUserInfo.getPdb() + "");
            card_name = mineUserInfo.getCard_name();
            card_num = mineUserInfo.getCard_num();
            pdb = mineUserInfo.getPdb();
            mobile = mineUserInfo.getMobile();
            realname = mineUserInfo.getRealname();
            head_pic1 = mineUserInfo.getHead_pic();
            pdb_num = mineUserInfo.getPdb();
            ingot = mineUserInfo.getIngot();
            tv_yuanbao.setText(mineUserInfo.getIngot() + "");
            tv_reputation.setText("我的信誉值：" + mineUserInfo.getReputation());

            RequestOptions options = new RequestOptions();
            options.error(R.mipmap.person_default);
            String head_pic = mineUserInfo.getHead_pic();
            Glide.with(mContext).load(head_pic)
                    .apply(options)
                    .into(ivHeader);


            int level = mineUserInfo.getLevel();
            switch (level) {
                case 0:
                    iv_level.setVisibility(View.GONE);
                    break;
                case 1:
                    iv_level.setVisibility(View.VISIBLE);
                    iv_level.setImageResource(R.mipmap.icon_geochronology);
                    break;
                case 2:
                    iv_level.setVisibility(View.VISIBLE);
                    iv_level.setImageResource(R.mipmap.icon_city_representative);
                    break;
                case 3:
                    iv_level.setVisibility(View.VISIBLE);
                    iv_level.setImageResource(R.mipmap.icon_rovincial_prepresentative);
                    break;
                case 4:
                    iv_level.setVisibility(View.VISIBLE);
                    iv_level.setImageResource(R.mipmap.icon_m4);
                    break;
                case 5:
                    iv_level.setVisibility(View.VISIBLE);
                    iv_level.setImageResource(R.mipmap.icon_m5);
                    break;
                case 6:
                    iv_level.setVisibility(View.VISIBLE);
                    iv_level.setImageResource(R.mipmap.icon_m6);
                    break;
                case 7:
                    iv_level.setVisibility(View.VISIBLE);
                    iv_level.setImageResource(R.mipmap.icon_m7);
                    break;
                case 8:
                    iv_level.setVisibility(View.VISIBLE);
                    iv_level.setImageResource(R.mipmap.icon_m8);
                    break;
                case 9:
                    iv_level.setVisibility(View.VISIBLE);
                    iv_level.setImageResource(R.mipmap.icon_m9);
                    break;
                case 10:
                    iv_level.setVisibility(View.VISIBLE);
                    iv_level.setImageResource(R.mipmap.icon_m10);
                    break;
                case 11:
                    iv_level.setVisibility(View.VISIBLE);
                    iv_level.setImageResource(R.mipmap.icon_m11);
                    break;
                case 12:
                    iv_level.setVisibility(View.VISIBLE);
                    iv_level.setImageResource(R.mipmap.icon_m12);
                    break;
                case 13:
                    iv_level.setVisibility(View.VISIBLE);
                    iv_level.setImageResource(R.mipmap.icon_m13);
                    break;
                case 14:
                    iv_level.setVisibility(View.VISIBLE);
                    iv_level.setImageResource(R.mipmap.icon_m14);
                    break;
                case 15:
                    iv_level.setVisibility(View.VISIBLE);
                    iv_level.setImageResource(R.mipmap.icon_m15);
                    break;


            }
        }

    }
}
